package com.canaan.business.schedule;

import com.canaan.business.common.enums.agv.AgvEnum;
import com.canaan.business.common.enums.inOutStock.InOutStockTypeEnum;
import com.canaan.business.common.enums.storehouse.StorehouseStockStatusEnum;
import com.canaan.business.domain.agv.AgvTaskDispatch;
import com.canaan.business.domain.basic.WmsStorehouseLocation;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.domain.vo.WmsStorehouseStockVo;
import com.canaan.business.service.agv.IAgvTaskDispatchService;
import com.canaan.business.service.basic.IWmsStorehouseLocationService;
import com.canaan.business.service.storehouse.IWmsStorehouseStockService;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component("AutoApplyEmptySchedule")
public class AutoApplyEmptySchedule {

    @Resource
    private IWmsStorehouseStockService storehouseStockService;
    @Resource
    private IAgvTaskDispatchService dispatchService;
    @Resource
    private IWmsStorehouseLocationService locationService;

    /**
     * 申请空料架
     */
    @ApiOperation(value = "申请空料架", notes = "申请空料架")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void apply() {
        //查询入库接驳空库位
        WmsStorehouseStockVo inDockingEmptyStockVo = storehouseStockService.selectInDockingEmptyWmsStorehouseStock();
        if (inDockingEmptyStockVo == null) {
            return;
        }
        WmsStorehouseLocation inDockingLocation = locationService.selectWmsStorehouseLocationByNo(inDockingEmptyStockVo.getStorehouseLocationNo());
        if (inDockingLocation == null) {
            return;
        }

        WmsStorehouseStock destStock = storehouseStockService.selectWmsStorehouseStockByLocationNo(inDockingLocation.getNo());
        if (destStock == null) {
            return;
        }
        /*
        //优先呼叫出库接驳空料架
        WmsStorehouseStockVo outDockingEmptyShelfStockVo=storehouseStockService.selectOutDockingEmptyShelfStorehouseStock();
        if(outDockingEmptyShelfStockVo!=null){
            WmsStorehouseLocation outDockingLocation=locationService.selectWmsStorehouseLocationByNo(outDockingEmptyShelfStockVo.getStorehouseLocationNo());
            if(outDockingLocation==null){
                return;
            }
            WmsStorehouseStock sourceStock=storehouseStockService.selectWmsStorehouseStockByLocationNo(outDockingLocation.getNo());
            if(sourceStock==null){
                return;
            }
            //创建AGV任务
            dispatchService.createTaskDispatch("",outDockingLocation,
                    inDockingLocation, AgvEnum.TASK_TYPE.APPLY_EMPTY,sourceStock.getContainerNo(),sourceStock.getTrayNo());
            sourceStock.setStatus(StorehouseStockStatusEnum.LOCK.getCode());
            storehouseStockService.updateWmsStorehouseStock(sourceStock);
            destStock.setStatus(StorehouseStockStatusEnum.LOCK.getCode());
            storehouseStockService.updateWmsStorehouseStock(destStock);
            return;
        }*/
        //呼叫成品库空料架（从行最大列最小）
        List<WmsStorehouseStockVo> productEmptyShelfStockVos = storehouseStockService.selectProductEmptyShelfStorehouseStock();
        if (productEmptyShelfStockVos != null) {
            WmsStorehouseStock sourceStock = null;
            WmsStorehouseLocation sourceLocation = null;
            for (WmsStorehouseStockVo item : productEmptyShelfStockVos) {
                sourceLocation = locationService.selectWmsStorehouseLocationByNo(item.getStorehouseLocationNo());
                if (sourceLocation == null) {
                    continue;
                }
                sourceStock = storehouseStockService.selectWmsStorehouseStockByLocationNo(sourceLocation.getNo());
                if (sourceStock == null) {
                    continue;
                }
                if ("0".equals(sourceLocation.getIsInner())) {
                    break;
                } else {
                    //外库点位
                    WmsStorehouseLocation outerLocation = locationService.selectWmsStorehouseLocationByNo(sourceLocation.getInnerLocation());
                    if (outerLocation == null) {
                        continue;
                    }
                    //外库库存
                    WmsStorehouseStock outerStock = storehouseStockService.selectWmsStorehouseStockByLocationNo(sourceLocation.getInnerLocation());
                    if (outerStock == null) {
                        continue;
                    }
                    //外库不是NORMAL状态
                    if (!(StorehouseStockStatusEnum.NORMAL.getCode().equals(outerStock.getStatus()))) {
                        continue;
                    }
                    //外库也是空料架，则选中外库
                    if (StringUtils.isNotEmpty(outerStock.getContainerNo()) && StringUtils.isEmpty(outerStock.getTrayNo())) {
                        sourceStock = outerStock;
                        sourceLocation = outerLocation;
                        break;
                    } else if (StringUtils.isEmpty(outerStock.getContainerNo()) && StringUtils.isNotEmpty(sourceStock.getContainerNo()) && StringUtils.isEmpty(sourceStock.getTrayNo())) {
                        //外库空点位，内库空料架，则选中内库
                        if (StorehouseStockStatusEnum.NORMAL.getCode().equals(sourceStock.getStatus())) {
                            //内库是NORMAL状态，选择内库
                            break;
                        }
                    }
                }
            }
            if (sourceStock == null || sourceLocation == null) {
                return;
            }
            if (!StringUtils.equals(sourceStock.getStorehouseLocationNo(), sourceLocation.getNo())) {
                log.error("点位查询和库位查询不是同一个位置库位：{}，点位：{}", sourceStock.getStorehouseLocationNo(), sourceLocation.getNo());
                return;
            }

            //创建AGV任务
            dispatchService.createTaskDispatch("", sourceLocation,
                    inDockingLocation, AgvEnum.TASK_TYPE.APPLY_EMPTY, sourceStock.getContainerNo(), sourceStock.getTrayNo());

            sourceStock.setStatus(StorehouseStockStatusEnum.LOCK.getCode());
            storehouseStockService.updateWmsStorehouseStock(sourceStock);
            destStock.setStatus(StorehouseStockStatusEnum.LOCK.getCode());
            storehouseStockService.updateWmsStorehouseStock(destStock);
        }
    }


}
